package edu.pw.p2pclient.networking;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiManager;

public class WifiReceiver extends BroadcastReceiver {

	private MessageSender mMessageSender;
	private IntentFilter mIntentFilter;

	public WifiReceiver(MessageSender messageSender) {

		mMessageSender = messageSender;
		mIntentFilter = new IntentFilter(
				WifiManager.NETWORK_STATE_CHANGED_ACTION);
		mIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
	}

	@Override
	public void onReceive(Context context, Intent intent) {

		if (intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION))
			mMessageSender.resetAddresses();
		if (intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
			int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
			if (WifiManager.WIFI_STATE_DISABLING == wifiState
					|| WifiManager.WIFI_STATE_DISABLED == wifiState) {

				CommunicationService.getInstance().handleDisconnect();
			}
		}
	}

	public IntentFilter getFilter() {
		return mIntentFilter;
	}

}
